/**
 * copyright(c) 2013 www.jiongshibaoliao.com, All Rights Reserved.
 * project: jiongshibaoliao 
 * author: 曾帅
 * date: 2014-1-2 下午02:09:17
 * version: v1.0
 */

package com.jiongshibaoliao.util;

import java.io.IOException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.log4j.Logger;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @project jiongshibaoliao
 * @author 曾帅
 * @version 1.0
 * @date 2014-1-2 下午02:09:17
 */

public class JiongDES {
	
	private static final Logger logger = Logger.getLogger(JiongDES.class);

	private static final String algorithm = "DESede";
	
	private static final String encoding = "UTF-8";
	
	private final static byte[] keyBytes = { 0x11, 0x22, 0x4F, 0x58,
			(byte) 0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, (byte) 0xCB,
			(byte) 0xDD, 0x55, 0x66, 0x77, 0x29, 0x74, (byte) 0x98, 0x30, 0x40,
			0x36, (byte) 0xE2 };

	private static Key getKey() {
		Key key = null;
		try {
			KeyGenerator _generator = KeyGenerator.getInstance(algorithm);
			_generator.init(new SecureRandom(keyBytes));
			key = _generator.generateKey();
			_generator = null;
		} catch (NoSuchAlgorithmException e) {
			logger.info("获取加密key异常：", e);
		}
		return key;
	}

	/**
	 * 加密
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-2 下午02:59:23
	 * @param strMing
	 * @return String
	 */
	public static String getencString(String strMing) {
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = "";

		BASE64Encoder encoder = new BASE64Encoder();
		try {
			byteMing = strMing.getBytes(encoding);
			byteMi = getEncCode(byteMing);
			strMi = encoder.encode(byteMi);
		} catch (Exception e) {
			logger.info("3DES加密异常：", e);
		} finally {
			encoder = null;
			byteMi = null;
			byteMing = null;
		}
		return strMi;
	}

	/**
	 * 解密
	 * @author 曾帅
	 * @version 1.0
	 * @date 2014-1-2 下午02:59:40
	 * @param strMi
	 * @return String
	 */
	public static String getDecString(String strMi) {
		BASE64Decoder base64Decoder = new BASE64Decoder();
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = "";
		try {
			byteMi = base64Decoder.decodeBuffer(strMi);
			byteMing = getDecCode(byteMi, JiongDES.getKey());
			strMing = new String(byteMing, encoding);
		} catch (IOException e) {
			logger.info("3DES解密异常：", e);
		} finally {
			base64Decoder = null;
			byteMing = null;
			byteMi = null;
		}
		return strMing;

	}

	private static byte[] getEncCode(byte[] byts) {
		byte[] byteFina = null;
		Cipher cipher;
		try {
			cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.ENCRYPT_MODE, JiongDES.getKey());
			byteFina = cipher.doFinal(byts);
		} catch (Exception e) {
			logger.info(e);
		} finally {
			cipher = null;
		}
		return byteFina;
	}

	private static byte[] getDecCode(byte[] bytd, Key key) {
		byte[] byteFina = null;
		Cipher cipher = null;
		try {
			cipher = Cipher.getInstance(algorithm);
			cipher.init(Cipher.DECRYPT_MODE, key);
			byteFina = cipher.doFinal(bytd);
		} catch (Exception e) {
			logger.info(e);
		} finally {
			cipher = null;
		}
		return byteFina;
	}

}
